package com.base.api.controller.PtTeam;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.exception.BaseException;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.result.ResultEnum;
import com.base.self.dto.ptTeam.PtCurrentVo;
import com.base.self.dto.ptTeam.PtCurrentVo2;
import com.base.self.entity.PtCurrent;
import com.base.self.service.IPtCurrentService;
import com.base.utils.Redis.RedisUtil;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-16
 */
@Api(tags = "拼团测试")
@RestController
@RequestMapping("/pt-current-test")
public class PtCurrentController {
    private final IPtCurrentService iPtCurrentService;
    private final RedisUtil redisCacheManager;

    public PtCurrentController(IPtCurrentService iPtCurrentService, RedisUtil redisCacheManager) {
        this.iPtCurrentService = iPtCurrentService;
        this.redisCacheManager = redisCacheManager;
    }

    @ApiOperation(value = "自动加团测试")
    @PostMapping("/autoInTeam")
    public ResponseResult autoInTeam(@ApiParam("用户ID") @RequestParam String userId
            , @ApiParam("开团人ID") @RequestParam String sub1
            , @ApiParam("商品ID") @RequestParam String goodsId
            , @ApiParam("参团订单号") @RequestParam String teamOrderNo
            , @ApiParam("订单号") @RequestParam String orderNo
    ) {
        return iPtCurrentService.autoInTeam(userId, sub1, goodsId,teamOrderNo,orderNo);
    }

    @ApiOperation(value = "创建团本")
    @PostMapping("/beginInTeanm")
    public ResponseResult beginInTeanm(@ApiParam("用户ID") @RequestParam String userId, @ApiParam("商品ID") @RequestParam String goodsId, @ApiParam("订单号") @RequestParam String orderNo) {
        return iPtCurrentService.beginInTeanm(userId, goodsId, orderNo);
    }

    @ApiOperation(value = "查询某个用户的分享团(分享页接口)")
    @PostMapping("/findTeamByUserId")
    public ResponseResult findTeamByUserId(@ApiParam("用户ID") @RequestParam String userId, @ApiParam("商品ID") @RequestParam String sid,@ApiParam("订单号") @RequestParam String orderNo) {
        ResponseResult responseResult = iPtCurrentService.findTeamByUserIdAndSid(userId, sid,orderNo, 1, 10);
        Object result = Optional.ofNullable(responseResult).filter(responseResult1 -> responseResult1.getCode() == ResultEnum.SUCCESS.getCode()).get().getData();
        IPage<PtCurrent> page = (IPage<PtCurrent>) result;
        List<PtCurrent> ptCurrents = Optional.ofNullable(page.getRecords()).orElseThrow(() -> new BaseException(ResponseResultUtils.getResponseResultF("暂无信息")));
        if (ptCurrents.size()<=0) throw new BaseException(ResponseResultUtils.getResponseResultF("暂无信息"));
        PtCurrentVo2 ptCurrentVo2 = iPtCurrentService.currentToVo2(ptCurrents.get(0));
        return ResponseResultUtils.getResponseResultDataS(ptCurrentVo2);
    }

    @ApiOperation(value = "查询某个用户的所有团(H5个人信息页接口)")
    @PostMapping("/findTeamByUserIdAndSid")
    public ResponseResult findTeamByUserIdAndSid(@ApiParam("用户ID") @RequestParam String userId,  @ApiParam("页码") @RequestParam Integer pageNo, @ApiParam("每页大小") @RequestParam Integer pageSize) {
        ResponseResult responseResult = iPtCurrentService.findTeamByUserIdAndSid(userId, null,null, pageNo, pageSize);
        Object result = Optional.ofNullable(responseResult).filter(responseResult1 -> responseResult1.getCode() == ResultEnum.SUCCESS.getCode()).get().getData();
        IPage<PtCurrent> page = (IPage<PtCurrent>) result;
        Map<String, Object> maps = Maps.newHashMap();
        maps.put("size", page.getSize());
//        maps.put("records", page.getRecords());
        maps.put("total", page.getTotal());
        maps.put("current", page.getCurrent());
        maps.put("pages", page.getPages());
        List<Object> resultList = Lists.newArrayList();
        page.getRecords().forEach(item -> {
            PtCurrentVo vo = iPtCurrentService.currentToVo(item);
            resultList.add(vo);
        });
        maps.put("resultList",resultList);
        return ResponseResultUtils.getResponseResultDataS(maps);
    }

    @ApiOperation(value = "查询用户积分明细")
    @PostMapping("/judgeTeamByUserIdAndSid")
    public ResponseResult judgeTeamByUserIdAndSid(@ApiParam("用户ID") @RequestParam String userId, @ApiParam("商品ID") @RequestParam String sid) {
        return iPtCurrentService.judgeTeamByUserIdAndSid(userId, sid);
    }
}

